package com.woniu.springcrud.controller;

import com.woniu.springcrud.entity.User;
import com.woniu.springcrud.enums.ResponseStatusEnum;
import com.woniu.springcrud.service.UserService;
import com.woniu.springcrud.vo.ResponseVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author: 16327
 * @date: 2021/1/18 15:38
 * @description:
 */
@RestController
public class UserController {
    @Autowired
    UserService userService;
    @PostMapping("/user/del/{id}")
    @ApiOperation("删除方法")
    public ResponseVo delUser(@PathVariable("id") Integer id){
        int i = userService.deleteUserById(id);
        if (i<1){
            return ResponseVo.errorDefaultMsg(ResponseStatusEnum.ERROR);
        }else {
            return ResponseVo.successWithMsgNoData("删除成功！");
        }
    }
    @PostMapping("/user/findAll")
    @ApiOperation("查询全部方法")
    public ResponseVo findAll(){
        List<User> all = userService.findAll();
        return ResponseVo.successNoMsgWithData(all);
    }
    @PostMapping("/user/update")
    @ApiOperation("更新方法")
    public ResponseVo updateUser(@RequestBody User user){
        int i = userService.updateUser(user);
        if (i<1){
            return ResponseVo.errorDefaultMsg(ResponseStatusEnum.ERROR);
        }else {
            return ResponseVo.successWithMsgNoData("修改成功！");
        }
    }
    @PostMapping("/user/insert")
    @ApiOperation("新增方法")
    public ResponseVo insertUser(@RequestBody User user){
        int i = userService.insertUser(user);
        if (i<1){
            return ResponseVo.errorDefaultMsg(ResponseStatusEnum.ERROR);
        }else {
            return ResponseVo.successWithMsgNoData("新增成功！");
        }
    }

}
